package com.example.demojdbcandthymeleaf.controller;


import com.example.demojdbcandthymeleaf.entity.Employee1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TestController1 {
    @Autowired(required = false)
    JdbcTemplate jdbcTemplate;

    @GetMapping("index")
    public String GetIndex(Model model){

        String sql = "select * from employee";
        List<Employee1> emps =  jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Employee1.class));
        model.addAttribute("emps",emps);

        return "index";

    }
//    @GetMapping("/index/{id}")
//    public String GetEmployeeByID(@PathVariable Integer id, Model model){
//        String sql = "select * from employee where id=?";
//        RowMapper<Employee1> rowMapper=new BeanPropertyRowMapper<>(Employee1.class);
//        Employee1 employee = jdbcTemplate.queryForObject(sql, rowMapper, id);
////        return employee;
//        model.addAttribute("employee",employee);
//        return "index";
//    }
}
